#include<iostream>
#include<assert.h>

using namespace std;

namespace refrain
{
	template<class T>
	struct list_node
	{
		list_node* _prev;
		list_node* _next;
		T _data;

		list_node(const T& x = T())
			: _prev(nullptr)
			, _next(nullptr)
			, _data(x)
		{}
	};
	template<class T, class Ref,class Ptr>
	struct list_iterator
	{
		typedef list_iterator<T, Ref, Ptr> Self;
		typedef list_node<T> Node;

		Node* _node;

		list_iterator(Node* node)
			:_node(node)
		{}
		Ptr operator->()
		{
			return &_node->_data;
		}
		Ref operator*()
		{
			return _node->_data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(_node);
			_node = _node->_next;
			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		Self operator--(int)
		{
			Self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		bool operator !=(const Self& s) const
		{
			return _node != s._node;
		}
		bool operator ==(const Self& s) const
		{
			return _node == s._node;
		}
	};
	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		list()
		{
			empty_init();
		}
		list(const list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		list(initializer_list<T> il)
		{
			empty_init();
			for (auto& e : it)
			{
				push_back(e);
			}
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void clear()
		{
			auto it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		void empty_init()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
			_size = 0;
		}
		void push_back(const T& x)
		{
			
			/*Node* tail = _head->_prev;
			Node* newnode = new Node(x);
			
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;*/
			
			insert(begin(), x);
		}
		void pop_back()
		{
			//tail del _head
			/*Node* del = _head->_prev;
			Node* tail = del->_prev;

			tail->_next = _head;
			_head->_prev = tail;

			delete del;*/

			erase(--end());
		}
		void insert(iterator pos, const T& x)
		{
			//prev newnode cur
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;

			_size++;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());

			//prev del next
			Node* del = pos._node;
			Node* prev = del->_prev;
			Node* next = del->_next;

			prev->_next = next;
			next->_prev = prev;

			delete del;
			--_size;
			return next;
		}
		
		size_t size()
		{
			return _size;
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
	private:
		Node* _head;
		size_t _size;
	};

	void test_list01()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		for (auto& it : lt)
		{
			cout << it << " ";
		}
		cout << endl;
		lt.pop_back();
		for (auto& it : lt)
		{
			cout << it << " ";
		}
		cout << endl;
		list<int> lt1;
		lt1 = lt;
		for (auto& it : lt)
		{
			cout << it << " ";
		}
	}
}


